package com.xyh.flowershop.config;

import com.xyh.flowershop.intercepts.AuthIntercept;
import com.xyh.flowershop.intercepts.CornsIntercept;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 小约翰可汗
 * @className MvcConfig
 * @createTime 2024/3/2421:18
 * MVC配置
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Value("${file.avatar-path}")
    private String avatarBasePath;

    @Value("${file.type-path}")
    private String typeBasePath;

    @Value("${file.goods-path}")
    private String goodsBasePath;

    @Value("${file.swipers-path}")
    private String swiperBasePath;

    @Value("${file.path}")
    private String basePath;

    private final AuthIntercept authIntercept;

    private final CornsIntercept cornsIntercept;

    public MvcConfig(AuthIntercept authIntercept, CornsIntercept cornsIntercept) {
        this.authIntercept = authIntercept;
        this.cornsIntercept = cornsIntercept;
    }

    /**
     * 注册资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**")
                .addResourceLocations("file:"+basePath);
        registry.addResourceHandler("/img/avatar/**")
                .addResourceLocations("file:"+avatarBasePath);
        registry.addResourceHandler("/img/type/**")
                .addResourceLocations("file:"+typeBasePath);
        registry.addResourceHandler("/img/swipers/**")
                .addResourceLocations("file:"+swiperBasePath);
        registry.addResourceHandler("/img/goods/**")
                .addResourceLocations("file:"+goodsBasePath);
        // 配置knife4j的资源路径
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:META-INF/resources/webjars/");
    }

    // 配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(cornsIntercept).addPathPatterns("/**");
        registry.addInterceptor(authIntercept).addPathPatterns("/**")
                .excludePathPatterns("/api/appIndex/**","/login/**","/goods/app/**","/type/app/**","/swipers/app/**",
                "/img/**","/doc.html/**", "/swagger-resources/**", "/v3/api-docs/**","/webjars/**","/comment/find/page");
//        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
